<?php

class DataTable extends PageElement
{

	protected $Database;
	protected $sql; // to be replaced by $select
	protected $select;
	protected $orderBy;
	protected $groupBy;
	protected $whereMethod = 'Conditions';
	protected $whereSQL;
	protected $ANDConditions = array(); // to replace the ones in the renderers
	protected $ORConditions = array();
	protected $filterRequests = array();
	protected $columns;
	protected $areViewable = false;
	protected $areEditable = false;
	protected $areDeletable = false;
	protected $areSelectable = false;
	protected $selectSourceTable = "";
	protected $editDeleteType = "Button"; // can be Button or Hyperlink
	protected $DBTableName = "";

// *********************************************
//Want to take these out and put them in filters
	protected $showDeleted = false;
	protected $showBefore = false;
	protected $showAfter = false;
	protected $showLocations = false;
	protected $showEventInvolvees = false;
	protected $showEventTypes = false;
// *********************************************

	protected $renderer;


	public function __construct()
	{
	}

	public function setRenderer($renderer)
	{
		$this->renderer = $renderer; 
	}

	public function setDatabase($db)
	{
		$this->Database = $db;
	}

	public function setSQL($sql)
	{
		$this->sql = $sql;
	}

	public function setSelect($select)
	{
		$this->select = $select;
	}

	public function setOrderBy($orderBy)
	{
		$this->orderBy = $orderBy;
	}
	
	public function setGroupBy($groupBy)
	{
		$this->groupBy = $groupBy;
	}
	
	public function setWhereMethod($whereMethod) // SQL or Conditions
	{
		$this->whereMethod = $whereMethod;
	}

	public function addANDCondition($condition)
	{
		if($condition<>'')
			array_push($this->ANDConditions,$condition);
	}

	public function addORCondition($condition)
	{
		if($condition<>'')
			array_push($this->ORConditions,$condition);
	}

	public function addFilterRequest($filterRequest)
	{
		if($filterRequest<>'')
		{
			array_push($this->filterRequests,$filterRequest);
//			echo $filterRequest->getFilterCondition()."<br>";
			// add associated OR or AND Condition
			if ($filterRequest->getIsORCondition())
				$this->addORCondition($filterRequest->getFilterCondition());
			else
				$this->addANDCondition($filterRequest->getFilterCondition());
		}

	}
	
	public function setWhereSQL($whereSQL)
	{
		$this->whereSQL = $whereSQL;
		
	}

	public function setColumns($columns)
	{
		$this->columns = $columns;
	}

	public function setEditable($editable)
	{
		$this->areEditable = $editable;
	}

	public function setDeletable($deletable)
	{
		$this->areDeletable = $deletable;
	}

	public function setViewable($viewable)
	{
		$this->areViewable = $viewable;
	}

	public function setSelectable($selectable)
	{
		$this->areSelectable = $selectable;
	}

	public function setSelectSourceTable($selectSourceTable)
	{
		$this->selectSourceTable = $selectSourceTable;
	}

	public function setEditDeleteType($type)
	{
		$this->editDeleteType = $type;
	}

	public function setDBTableName($table)
	{
		$this->DBTableName = $table;
	}

	public function showDeleted($showDeleted)
	{
		$this->showDeleted = $showDeleted;
	}

	public function showBefore($showBefore)
	{
		$this->showBefore = $showBefore;
	}

	public function showAfter($showAfter)
	{
		$this->showAfter = $showAfter;
	}

	public function showLocations($showLocations)
	{
		$this->showLocations = $showLocations;
	}

	public function showEventInvolvees($showEventInvolvees)
	{
		$this->showEventInvolvees = $showEventInvolvees;
	}

	public function showEventTypes($showEventTypes)
	{
		$this->showEventTypes = $showEventTypes;
	}

	public function getSQL() {return $this->sql;}
	public function getDatabase() {return $this->Database;}
	public function getShowDeleted() {return $this->showDeleted;}
	public function getShowBefore() {return $this->showBefore;}
	public function getShowAfter() {return $this->showAfter;}
	public function getShowLocations() {return $this->showLocations;}
	public function getShowEventInvolvees() {return $this->showEventInvolvees;}
	public function getShowEventTypes() {return $this->showEventTypes;}
	public function getColumns() {return $this->columns;}
	public function getAreEditable() {return $this->areEditable;}
	public function getAreDeletable() {return $this->areDeletable;}
	public function getAreViewable() {return $this->areViewable;}
	public function getAreSelectable() {return $this->areSelectable;}
	public function getSelectSourceTable() {return $this->selectSourceTable;}
	public function getEditDeleteType() {return $this->editDeleteType;}
	public function getDBTableName() {return $this->DBTableName;}
	public function getOrderBy() {return $this->orderBy;}
	
	private function getOrderBySQL() 
	{
		return " ORDER BY ".$this->orderBy;
	}

	private function getGroupBySQL() 
	{
		if (isset($this->groupBy))
			return " GROUP BY ".$this->groupBy;
	}

	private function getSelectSQL() 
	{
		return $this->select;
	}
	
	public function getWhereSQLClause()
	{
//		echo "Where method = ".$this->whereMethod."<br>";
//		echo "Where SQL = ".$this->getWhereSQL()."<br>";
		if($this->whereMethod == 'Conditions')
			return $this->getWhereConditions();
		if($this->whereMethod == 'SQL')
			return $this->getWhereSQL();
	}
	
	protected function getWhereConditions()
	{
		$ANDConditions = "";
		foreach ($this->ANDConditions as $condition)
		{
			$ANDConditions .= $condition." AND ";
		}
		$ANDConditions = substr($ANDConditions,0,-4);

		$ORConditions = "";
		foreach ($this->ORConditions as $condition)
		{
			$ORConditions .= $condition." OR ";
		}
		$ORConditions = substr($ORConditions,0,-3);
		
		if ($ANDConditions == "") $ANDConditions = '1';
		if ($ORConditions == "") $ORConditions = '1';
		

		return " WHERE (".$ANDConditions.") AND (".$ORConditions.") ";
	}


	protected function getWhereSQL()
	{
		return $this->whereSQL;
	}
	
	public function getSQLCommand()
	{
		return $this->getSelectSQL().$this->getWhereSQLClause().$this->getGroupBySQL().$this->getOrderBySQL();
	}

	public function getFilterRequests()
	{
		return $this->filterRequests;
	}


	public function getHTML()
	{

		$html = $this->renderer->getHTML();
		return $html;
	}
}

?>
